#include "my_headerT2csJ.h"

//v1.0

int trans_file(int netfd){
    train_t tnt = {5,"file2"};
    send(netfd,&tnt,tnt.msglen+sizeof(int),MSG_NOSIGNAL);

    int opfd = open("file2",O_RDWR);
    
    struct stat sta;
    fstat(opfd,&sta);
    off_t allsize = sta.st_size;
    send(netfd,&allsize,sizeof(allsize),MSG_NOSIGNAL);
    
    char *mmapFile =(char *)mmap(NULL,allsize,PROT_WRITE|PROT_WRITE,MAP_SHARED,opfd,0);
    int offsets = 0;

    sleep(10);

    while(offsets<allsize){
        if(allsize - offsets <1004){
            bzero(&tnt,sizeof(tnt));
            tnt.msglen = allsize -offsets;
            memcpy(tnt.msg,mmapFile+offsets,tnt.msglen);
            send(netfd,&tnt,sizeof(int)+tnt.msglen,MSG_NOSIGNAL);
            break;
        }
        bzero(&tnt,sizeof(int));
        tnt.msglen = 1004;
        memcpy(tnt.msg,mmapFile+offsets,tnt.msglen);
        send(netfd,&tnt,sizeof(int)+tnt.msglen,MSG_NOSIGNAL);
        offsets+=1004;
    }
    int closenum = 0;
    send(netfd,&closenum,sizeof(int),MSG_NOSIGNAL);
    munmap(mmapFile,allsize);
    close(opfd);
    return 0;
}
